﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RegistrationController;

namespace HPMDesktop.Patient
{
    public partial class frmShiftOutPatient : Form
    {
        private string _patientName;
        private string _CRNumber;
        private string _consultantName;
        private Hashtable _consultantInfo;
        private string _registrationFees = "0";
        private frmPatientDetails _objParentForm;
        
        public frmShiftOutPatient(frmPatientDetails objParentForm,
                                  string patientName, 
                                  string CRNumber, 
                                  string consultantName)
        {
            InitializeComponent();
            _patientName = patientName;
            _CRNumber = CRNumber;
            _consultantName = consultantName;
            _objParentForm = objParentForm;
        }

        private void frmShiftOutPatient_Load(object sender, EventArgs e)
        {
            txtRegFees.Text = "0";
            txtDeposit.Text = "0";
            txtTotal.Text = "0";

            /* Load consultants from db */
            ConsultantInfo_DataLoader objCIDataLoader = new ConsultantInfo_DataLoader();

            _consultantInfo = objCIDataLoader.Get_ConsultantFees_Data();

            foreach (String consultantName in _consultantInfo.Keys)
            {
                chkConsultant.Items.Add(consultantName);
            }

            objCIDataLoader.Dispose();

            /* Get Registration Fees from db */
            GlobalData_DataLoader objGDDataLoader = new GlobalData_DataLoader();
            GlobalData objGlobalData = objGDDataLoader.Get_Global_Data();
            if (objGlobalData != null &&
                objGlobalData.RegistrationFees != null)
            {
                _registrationFees = objGlobalData.RegistrationFees;
            }

            objGlobalData.Dispose();
            objGDDataLoader.Dispose();

            /* set fields on form */
            txtpname.Text = _patientName;
            txtCRNum.Text = _CRNumber;
            txtRegFees.Text = _registrationFees;
            chkConsultant.Text = _consultantName;
            txtTotal.Text = Convert.ToString(
                            Convert.ToDecimal(txtRegFees.Text) +
                            Convert.ToDecimal(txtDeposit.Text));
        }

        private void cmdSelectPkg_Click(object sender, EventArgs e)
        {
            frmSelectPackage frm = new frmSelectPackage(this, "frmShiftOutPatient");
            frm.Show();
        }

        private void cmdClearPkg_Click(object sender, EventArgs e)
        {
            txtPkgName.Text = "";
            txtPkgAmount.Text = "";
        }

        public void setPackageDetails(string packageName, string packageAmount)
        {
            txtPkgName.Text = packageName;
            txtPkgAmount.Text = packageAmount;
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            PatientRegistration objPatientRegistration = new PatientRegistration();
            objPatientRegistration.CRNumber = txtCRNum.Text;
            objPatientRegistration.Consultant = chkConsultant.Text;
            objPatientRegistration.RegistrationFees = txtRegFees.Text;
            objPatientRegistration.InitialDeposit = txtDeposit.Text;
            objPatientRegistration.PackageName = txtPkgName.Text;
            objPatientRegistration.PatientType = "I";

            PatientRegistration_Mapper objPRmapper = new PatientRegistration_Mapper();
            if (objPRmapper.Shift_OutPatient_to_InPatient(objPatientRegistration))
            {
                _objParentForm.shift_outpatient_to_inpatient(objPatientRegistration.Consultant);
                _objParentForm.setPackageDetails(txtPkgName.Text, txtPkgAmount.Text);
                objPatientRegistration.Dispose();
                objPRmapper.Dispose();
                this.Close();
            }
            else
            {
                MessageBox.Show("Error in transferring Out-Patient to In-Patient. Try again after some time.");
                objPatientRegistration.Dispose();
                objPRmapper.Dispose();
            }
        }

        private void chkPaid_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPaid.Checked.Equals(true))
            {
                cmdOk.Enabled = true;
            }
            else
            {
                cmdOk.Enabled = false;
            }
        }

        private void txtRegFees_LostFocus(object sender, EventArgs e)
        {
            /* TODO : Getting Lost Focus events multiple times,
             * need to check this
             */
            if (txtRegFees.Text.Equals(""))
            {
                txtRegFees.Text = "0";
            }
            if (txtDeposit.Text.Equals(""))
            {
                txtDeposit.Text = "0";
            }
            txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
        }

        private void txtDeposit_LostFocus(object sender, EventArgs e)
        {
            /* TODO : Getting Lost Focus events multiple times,
             * need to check this
             */
            if (txtRegFees.Text.Equals(""))
            {
                txtRegFees.Text = "0";
            }
            if (txtDeposit.Text.Equals(""))
            {
                txtDeposit.Text = "0";
            }
            txtTotal.Text = Convert.ToString(
                                Convert.ToDecimal(txtRegFees.Text) +
                                Convert.ToDecimal(txtDeposit.Text));
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
